# encoding = utf-8
import numpy as np

from application.utils.CodeTimingUtil import CodeTimingUtil

"""
平均绝对百分比误差（Mean Absolute Percentage Error）
范围[0,+∞), MAPE为0%表示完美模型, MAPE大于100%则表示劣质模型。
注意点: 当真实值有数据等于0是, 存在分母0除问题, 该公式不可用！

公式：
----------------------------------------------------------
MAPE = (1/n) * Σ(|actual – prediction| / |actual|) * 100
where:

Σ – a symbol that means “sum”
n – sample size
actual – the actual data value
prediction – the predicted data value
----------------------------------------------------------

参考文档-1:
预测评价指标RMSE、MSE、MAE、MAPE、SMAPE
https://blog.csdn.net/guolindonggld/article/details/87856780

参考文档-2:
How to Calculate MAPE in Python
https://www.statology.org/mape-python/
"""


# TODO MAPE 多少值算是合适
@CodeTimingUtil(name="[评分计算]mape")
def mape(y_true, y_pred):
    """
    参数:
    y_true -- 测试集目标真实值
    y_pred -- 测试集目标预测值

    返回:
    mape -- MAPE 评价指标
    """
    y_pred = np.array(list(y_pred))
    y_true = np.array(list(y_true))
    n = len(y_true)
    #
    mape_ = np.mean(np.abs((y_true - y_pred) / y_true)) * 100
    # mape = sum(np.abs((y_true - y_pred) / y_true)) / n * 100
    return '%.3f' % mape_
    pass


if __name__ == '__main__':
    pass
